//
//  BattleManager.cpp
//  xyz
//
//  Created by jorsun on 14-3-14.
//
//

#include "BattleManager.h"
static BattleManager* m_pInstance;
BattleManager::BattleManager(){
    scene=new BattleScene();
    scene->schedule(schedule_selector(BattleManager::frame),0.03f);
};
 BattleManager::~BattleManager(void)
{
}
 BattleManager* BattleManager::getInstance(){
    if (m_pInstance==NULL ){
        m_pInstance = new BattleManager();
    }
    return m_pInstance;
}
void BattleManager::addPlayer(BattleObject *battleObject){
    battleObjs.insert(pair<std::string,BattleObject*>(battleObject->getName(),battleObject));
}
void  BattleManager::frame(float f){
    map <std::string,BattleObject*>::iterator m1_Iter;
    for ( m1_Iter = battleObjs.begin( ); m1_Iter != battleObjs.end( ); m1_Iter++ ){
        BattleObject* battleObject=m1_Iter->second;
        battleObject->frame();
    }
}
void BattleManager::move(MoveMessage* moveMessage){
    BattleObject* obj=battleObjs[moveMessage->getName()];
    if(obj!=NULL){
        obj->setPosition(ccp(moveMessage->getX(),moveMessage->getY()));
    }
}
void BattleManager::enterBattle(){
    scene->setPlayer(CommonUtil::getInstance()->getPlayer());
    scene->addChild(CommonUtil::getInstance()->getPlayer()->getViewObj());
    CCDirector::sharedDirector()->replaceScene(scene);
}
void BattleManager::addPlayer(BattleAddPlayerMessage* msg){
    BattleObject* obj=new BattleObject();
    CCArmature *armature = CCArmature::create("girl");
    obj->init(msg->getName(),msg->getX(),msg->getY(),armature,STAND,RIGHT);
    scene->addChild(obj->getViewObj());
}

